#include <iostream>
#include <memory>

// Strategy Interface: 定义所有支持的算法的公共接口。
class Strategy {
public:
    virtual ~Strategy() {}
    virtual void execute() const = 0;
};

// ConcreteStrategyA: 实现策略接口的具体算法A
class ConcreteStrategyA : public Strategy {
public:
    void execute() const override {
        std::cout << "Executing strategy A." << std::endl;
    }
};

// ConcreteStrategyB: 实现策略接口的具体算法B
class ConcreteStrategyB : public Strategy {
public:
    void execute() const override {
        std::cout << "Executing strategy B." << std::endl;
    }
};

// Context: 维护一个对策略对象的引用
class Context {
private:
    std::unique_ptr<Strategy> strategy; // 使用智能指针管理策略对象

public:
    Context(Strategy* strategy = nullptr) : strategy(strategy) {}

    ~Context() {}

    // 允许替换当前的策略对象
    void setStrategy(Strategy* newStrategy) {
        strategy.reset(newStrategy);
    }

    // 执行策略
    void executeStrategy() const {
        if (strategy) {
            strategy->execute();
        }
    }
};

int main() {
    Context context;

    // 使用动态分配的策略对象
    context.setStrategy(new ConcreteStrategyA());
    context.executeStrategy();

    // 切换到策略B
    context.setStrategy(new ConcreteStrategyB());
    context.executeStrategy();

    return 0;
}
